package handler

import (
	"github.com/gin-gonic/gin"
	pn "lab-service/proto/notice"
	"net/http"
)

type Notice struct {
	NoticeClient pn.NoticeService
}

func (m *Notice) One(ctx *gin.Context) {
	form := new(pn.NoticeRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	rsp, err := m.NoticeClient.One(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}

	ctx.JSON(http.StatusOK, rsp)
}

func (m *Notice) Page(ctx *gin.Context) {
	form := new(pn.NoticeRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}
	form.Enable = true

	rsp, err := m.NoticeClient.Page(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}

	ctx.JSON(http.StatusOK, rsp)
}

// only admin
func (m *Notice) PageAll(ctx *gin.Context) {
	form := new(pn.NoticeRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	rsp, err := m.NoticeClient.Page(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}

	ctx.JSON(http.StatusOK, rsp)
}

// only admin
func (m *Notice) Create(ctx *gin.Context) {
	form := new(pn.NoticeRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	_, err := m.NoticeClient.Create(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}

	ctx.JSON(http.StatusOK, "success")
}

func (m *Notice) Toggle(ctx *gin.Context) {
	form := new(pn.NoticeRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	_, err := m.NoticeClient.Toggle(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}

	ctx.JSON(http.StatusOK, "success")
}
